package com.employee.service.impl;

import com.employee.context.BaseContext;
import com.employee.exception.PosExistException;
import com.employee.mapper.PosMapper;
import com.employee.pojo.dto.PositionDto;
import com.employee.pojo.dto.UpdatePosDto;
import com.employee.pojo.entry.PosHistory;
import com.employee.pojo.entry.Position;
import com.employee.pojo.vo.PosStaffVo;
import com.employee.service.PosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class PosServiceImpl implements PosService {
    @Autowired
    private PosMapper posMapper;

    @Override
    public List<PosHistory> getPosList(Long id) {
        return posMapper.getPosList(id);
    }

    @Override
    public List<Position> getPositionList() {
        return posMapper.getPositionList();
    }

    @Override
    public Position getPosById(Long id) {
        return posMapper.getPositionById(id);
    }

    @Override
    public List<PosStaffVo> getStaffList(Long id) {
        List<PosStaffVo> staffList = posMapper.getStaffList(id);
        for (PosStaffVo staff : staffList) {
            LocalDate birth = staff.getBirth();
            Integer age = LocalDate.now().getYear() - birth.getYear();// 计算年龄
            staff.setAge(age);
        }
        return staffList;
    }

    @Override
    public void addPosition(PositionDto positionDto) {
        Position position = posMapper.selectPosByName(positionDto.getPosName());
        if (position == null) {
            posMapper.addPosition(positionDto);
        } else {
            throw new PosExistException("职位已存在");
        }
    }

    @Override
    public void updatePosition(UpdatePosDto positionDto) {


        posMapper.updatePosition(positionDto);


    }

    @Override
    public List<Position> getPosListByQuery(String posName, String status) {
        if (status != null && !status.isEmpty()) {
            if (status.equals("1")) {
                status = "活动";
            }
            if (status.equals("2")) {
                status = "撤销";
            }
        }
        return posMapper.getPosListByQuery(posName, status);
    }

    @Override
    public void updateStatus(Long posId, String status) {
        if (status.equals("1")) {
            status = "活动";
        }
        if (status.equals("2")) {
            status = "撤销";
        }
        posMapper.updateStatus(posId,status);
    }
}
